package app.kotlin_1.adapter

import android.content.Context
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView

import android.view.View
import android.view.ViewGroup
import app.kotlin_1.R
import app.kotlin_1.activity.GameDetailActivity
import app.kotlin_1.activity.GameKindActivit
import app.kotlin_1.bean.GameBean
import app.kotlin_1.utils.inflate
import app.kotlin_1.utils.toast
import  kotlinx.android.synthetic.main.item_product_header.view.*
import  kotlinx.android.synthetic.main.item_product_title.view.*
import  kotlinx.android.synthetic.main.item_product_content.view.*
import org.jetbrains.anko.startActivity


/**
 * Created by 刘智强 on 2017/10/9.
 */
class ProductAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private val HEADER = 0
    private val TITLE = 1
    private val CONTENT = 2
    private var context: Context? = null
    private var datas = mutableListOf<GameBean>()

    constructor(context: Context?, datas: MutableList<GameBean>) : super() {
        this.context = context
        this.datas = datas
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder =
            when (viewType) {
                HEADER -> HeaderViewHolder(parent?.inflate(R.layout.item_product_header))
                TITLE -> TitleViewHolder(parent?.inflate(R.layout.item_product_title))
                CONTENT -> ContentViewHolder(parent?.inflate(R.layout.item_product_content))
                else -> super.createViewHolder(parent, viewType)
            }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        when (holder) {
            is HeaderViewHolder -> setHeaderData(holder)
            is TitleViewHolder -> setTitleData(holder, position - 1)
            is ContentViewHolder -> setContentData(holder, position - 1)

        }
    }

    override fun getItemCount(): Int = datas.size + 1

    override fun getItemViewType(position: Int): Int {
        var type: Int? = null
        when {
            position == 0 -> type = HEADER
            datas[position - 1].type == 0 -> type = TITLE
            datas[position - 1].type == 1 -> type = CONTENT
        }

        return type!!
    }

    private fun setHeaderData(holder: HeaderViewHolder?) {
        holder?.view?.ivSearch?.setOnClickListener { context?.toast(holder?.view?.etSearch?.text?.trim().toString()) }
        var kind = mutableListOf<GameBean>()
        kind.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505995323225&di=103badf27b8ae29cf925689a8f6f0eec&imgtype=0&src=http%3A%2F%2Fpic100.nipic.com%2Ffile%2F20160605%2F23324175_111518288000_2.jpg", "植物大战僵尸"))
        kind.add(GameBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1027189787,3617982638&fm=27&gp=0.jpg", "部落冲突"))
        kind.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505995405101&di=db1cd91aac9d1f42324791aba259657e&imgtype=0&src=http%3A%2F%2Fa1.att.hudong.com%2F17%2F87%2F20300543887706146664874036763.jpg", "皇室战争"))
        kind.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505995437926&di=5d2364d8619db672df5c9f7e20c03306&imgtype=0&src=http%3A%2F%2Fpic3.16pic.com%2F00%2F17%2F90%2F16pic_1790648_b.jpg", "保卫萝卜"))
        holder?.view?.rvHeaderKind?.layoutManager = GridLayoutManager(context, 4)
        holder?.view?.rvHeaderKind?.adapter = GameKindSheJiAdapter(context, kind, R.layout.item_gallery)
    }

    private fun setTitleData(holder: TitleViewHolder?, position: Int) {
        holder?.view?.tvTitle?.text = datas[position].name
        holder?.view?.rlTitle?.setOnClickListener {
            var title=datas[position].name
            context?.startActivity<GameKindActivit>("title" to "$title" )

        }
    }

    private fun setContentData(holder: ContentViewHolder?, position: Int) {
        holder?.view?.tvNumber?.text = datas[position].number.toString()
        holder?.view?.sdvGameIcon?.setImageURI(datas[position].icon)
        holder?.view?.tvName?.text = datas[position].name
        holder?.view?.tvDescription?.text = datas[position].desc
        holder?.view?.tvMemory?.text = datas[position].memory
        holder?.view?.llRootView?.setOnClickListener {
            context?.startActivity<GameDetailActivity>("game" to datas[position])
        }
    }

    inner class HeaderViewHolder : RecyclerView.ViewHolder {
        var view: View? = null

        constructor(itemView: View?) : super(itemView) {
            view = itemView
        }
    }

    inner class TitleViewHolder : RecyclerView.ViewHolder {
        var view: View? = null

        constructor(itemView: View?) : super(itemView) {
            view = itemView
        }
    }

    inner class ContentViewHolder : RecyclerView.ViewHolder {
        var view: View? = null

        constructor(itemView: View?) : super(itemView) {
            view = itemView
        }
    }

}